package com.demo.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 
 * @ClassName: DruidConfiguration
 * @Description: 配置数据源datasource
 * @author wqq
 * @date 2016年9月19日 下午1:51:05
 *
 */
@Configuration
public class DruidConfiguration {

	@Bean(name = "ztm", initMethod = "init", destroyMethod = "close")
	public DataSource compare2DataSource(
			@Value("${spring.datasource.ztm.driver-class-name}") String driver,
			@Value("${spring.datasource.ztm.url}") String url,
			@Value("${spring.datasource.ztm.username}") String username,
			@Value("${spring.datasource.ztm.password}") String password,
			@Value("${spring.datasource.ztm.minIdle}") int minIdle,
			@Value("${spring.datasource.ztm.maxActive}") int maxActive,
			@Value("${spring.datasource.ztm.initialSize}") int initialSize,
			@Value("${spring.datasource.ztm.timeBetweenEvictionRunsMillis}") long timeBetweenEvictionRunsMillis,
			@Value("${spring.datasource.ztm.minEvictableIdleTimeMillis}") long minEvictableIdleTimeMillis,
			@Value("${spring.datasource.ztm.validationQuery}") String validationQuery,
			@Value("${spring.datasource.ztm.testWhileIdle}") boolean testWhileIdle,
			@Value("${spring.datasource.ztm.testOnBorrow}") boolean testOnBorrow,
			@Value("${spring.datasource.ztm.testOnReturn}") boolean testOnReturn) {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName(driver);
		druidDataSource.setUrl(url);
		druidDataSource.setUsername(username);
		druidDataSource.setPassword(password);
		druidDataSource.setMinIdle(minIdle);
		druidDataSource.setMaxActive(maxActive);
		druidDataSource.setInitialSize(initialSize);
		druidDataSource
				.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		druidDataSource
				.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		druidDataSource.setValidationQuery(validationQuery);
		druidDataSource.setTestWhileIdle(testWhileIdle);
		druidDataSource.setTestOnBorrow(testOnBorrow);
		druidDataSource.setTestOnReturn(testOnReturn);
		return druidDataSource;
	}

}
